package okw.communication;

import java.net.MalformedURLException;
import java.util.HashMap;

import org.jboss.remoting.InvokerLocator;

import core.communication.message.Message;
import core.database.NodeType;

public class Client {
	private org.jboss.remoting.Client client = null;
	private String locatorURI = null;
	private NodeType type = null;
	private Boolean isWorking = true;

	public Client(String locatorURI, NodeType type) {
		try {
			this.type = type;
			this.locatorURI = locatorURI;
			InvokerLocator invokerLocator;
			invokerLocator = new InvokerLocator("socket://" + locatorURI);
			client = new org.jboss.remoting.Client(invokerLocator, locatorURI);
			client.connect();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void send(Message message) throws Throwable {
		HashMap metadata = new HashMap();
		metadata.put("timeout", "2000");
		this.client.invoke(message, metadata);
	}

	public void disconnect() {
		this.client.disconnect();
	}

	public Boolean getIsWorking() {
		return isWorking;
	}

	public void setIsWorking(Boolean isWorking) {
		this.isWorking = isWorking;
	}

	public String getLocatorURI() {
		return locatorURI;
	}

	public NodeType getType() {
		return type;
	}
}
